package com.iptv.grape.bu.factory;

import android.view.KeyEvent;

import com.iptv.grape.bu.AppState;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.bu.IKeyEventPorc;
import com.iptv.grape.bu.imp.*;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

public class BusinessLogicBuilder {

    static List<Class> classList = new ArrayList<>();

    static {
        classList.add(QuitFullScreen.class);
        classList.add(ShowLiveMenu.class);
        classList.add(HiddLiveMenu.class);
        classList.add(QuitVodFullScreen.class);
        classList.add(QuitBackMode.class);
        classList.add(HiddBillList.class);
        classList.add(PopBackMenu.class);
        classList.add(PausePlay.class);
        classList.add(VodPausePlay.class);
        classList.add(ShowVodMenu.class);
        classList.add(HiddVodMenu.class);
        classList.add(VodPausePlay2.class);
        classList.add(ShowRecommand.class);
        classList.add(HiddRecommand.class);
        classList.add(ShowTimeLiveUI.class);
        classList.add(QuitTimeLiveMode.class);
        classList.add(QuitTimeLiveMode2.class);
        classList.add(ShowTimeLiveUI2.class);
        classList.add(ChangeLiveGoUp.class);
        classList.add(ChangeLiveGoDown.class);
        classList.add(StartChangingLive.class);
        classList.add(StartChangingLive2.class);
        classList.add(StartChangingLiveNow.class);
    }

    public static IKeyEventPorc createProc(KeyEvent event){

        for(int index = 0; index < classList.size(); index++){
            Class c = classList.get(index);
            Annotation[] annotations = c.getAnnotations();
            for(Annotation ann:annotations){
                if(AppState.class.isAssignableFrom(ann.getClass())) {
                    AppState appState = (AppState) ann;
                    // 判断是否和当前的应用程序状态一直
                    if(isEqualAppState(appState,event))
                    {
                        IKeyEventPorc proc = null;
                        try {
                            proc = (IKeyEventPorc)c.newInstance();
                        }
                        catch (Exception ex){
                            ex.printStackTrace();
                            proc = null;
                        }
                        return proc;
                    }
                }
            }

        }
        //PlayStatus playStatus = test.getClass().getAnnotation(PlayStatus.class);
        return null;
    }


    private static boolean isEqualAppState(AppState appState,KeyEvent event){
       boolean isAppState = GrapeContext.curGrapeContext().isEqualAppState(appState);
       if(isAppState) {
           int[] array = appState.keyVals();
           for (int i = 0; i < array.length; i++) {
               if (array[i] == event.getKeyCode()) {
                    return true;
               }
           }
           return false;
       }
       return false;
    }

}
